package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.it2297.controller.JobController;
import com.it2297.main.CompanyApp;

public class JobRemoveJob extends JFrame {

	private JPanel contentPane;

	
	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen;
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	
	
	private JLabel lbl_title;
	private JLabel lbl1;
	private JLabel lbl2;
	private JLabel lbl3;
	private JLabel lbl4;
	private JLabel lbl5;
	private JLabel lbl7;
	private JLabel lbl8;
	private JLabel lbl9;
	private JLabel lbl10;
	private JTextField tf_desc;
	private JTextField tf_req;
	private JTextField tf_duty;
	private JTextField tf_it;
	private JTextField tf_et;
	private JTextField tf_wst;
	private JTextField tf_wet;
	private JTextField tf_salary;
	private JTextField tf_benefit;
	private JTextField tf_cname;
	private JLabel lblTo;
	private JLabel lblcriteria; 
	private JLabel lblNewLabel_2;
	private String cname;
	private String title;
	private JButton btnRemove;
	private JComboBox cb_title;
	final JobController jct= new JobController(this);
	private JTextField tf_jobid;
	
	
	
	
	
	public String getTitle(){
		 
        return cb_title.getSelectedItem().toString();

	}
	public String getJobId(){
		return tf_jobid.getText();
	}
	public void setDescription(String desc){
		tf_desc.setText(desc);
	}
	public void setRequirement(String req){
		tf_req.setText(req);
	}
	public void setDuty(String duty){
		tf_duty.setText(duty);
	}
	public void setIndustryType(String it){
		tf_it.setText(it);
	}
	public void setEmploymentType(String et){
		tf_et.setText(et);
	}
	public void setStartTime(String wst){
		tf_wst.setText(wst);
	}
	public void setEndTime(String wet){
		tf_wet.setText(wet);
	}
	public void setSalary(String salary){
		tf_salary.setText(salary);
	}
	public void setBenefit(String benefit){
		tf_benefit.setText(benefit);
	}
	public void setCompanyName(String cname){
		tf_cname.setText(cname);
	}
	public void setJobId(String jobid){
		tf_jobid.setText(jobid);
	}
	
	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JobRemoveJobTest frame = new JobRemoveJobTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public JobRemoveJob() {
		setTitle("Company - Remove Job Advertisement ");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Remove Job Advertisement");
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 35));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(0, 35, 724, 55);
		getContentPane().add(lblNewLabel);
		
	
		
		lbl_title = new JLabel("Job Title:");
		lbl_title.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl_title.setBounds(90, 101, 111, 23);
		getContentPane().add(lbl_title);
		
		tf_desc = new JTextField();
		tf_desc.setEditable(false);;
		tf_desc.setBounds(229, 205, 467, 23);
		getContentPane().add(tf_desc);
		tf_desc.setColumns(10);
		
		lbl1 = new JLabel("Job Description:");
		lbl1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl1.setBounds(90, 203, 140, 23);
		getContentPane().add(lbl1);
		
		tf_req = new JTextField();
		tf_req.setEditable(false);
		tf_req.setBounds(229, 239, 467, 23);
		getContentPane().add(tf_req);
		tf_req.setColumns(10);
		
		lbl2 = new JLabel("Job Requirement:");
		lbl2.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl2.setBounds(90, 239, 140, 19);
		getContentPane().add(lbl2);
		
		tf_duty = new JTextField();
		tf_duty.setEditable(false);
		tf_duty.setBounds(229, 273, 467, 23);
		getContentPane().add(tf_duty);
		tf_duty.setColumns(10);
		
		lbl3 = new JLabel("Duty:");
		lbl3.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl3.setBounds(90, 273, 111, 19);
		getContentPane().add(lbl3);
		
		tf_it = new JTextField();
		tf_it.setEditable(false);
		tf_it.setBounds(229, 307, 172, 23);
		getContentPane().add(tf_it);
		tf_it.setColumns(10);
		
		lbl4 = new JLabel("Industry Type:");
		lbl4.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl4.setBounds(90, 305, 111, 23);
		getContentPane().add(lbl4);
		
		tf_et = new JTextField();
		tf_et.setEditable(false);
		tf_et.setBounds(229, 343, 125, 23);
		getContentPane().add(tf_et);
		tf_et.setColumns(10);
		
		lbl5 = new JLabel("Employment Type:");
		lbl5.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl5.setBounds(90, 341, 140, 23);
		getContentPane().add(lbl5);
		
		tf_wst = new JTextField();
		tf_wst.setEditable(false);
		tf_wst.setBounds(229, 377, 125, 22);
		getContentPane().add(tf_wst);
		tf_wst.setColumns(10);
		
		lbl7 = new JLabel("Working Time:");
		lbl7.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl7.setBounds(90, 375, 153, 23);
		getContentPane().add(lbl7);
		
		tf_wet = new JTextField();
		tf_wet.setEditable(false);
		tf_wet.setBounds(392, 377, 125, 22);
		getContentPane().add(tf_wet);
		tf_wet.setColumns(10);
		
		lbl8 = new JLabel("Salary(SGD($)):");
		lbl8.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl8.setBounds(90, 409, 111, 23);
		getContentPane().add(lbl8);
		
		tf_salary = new JTextField();
		tf_salary.setEditable(false);
		tf_salary.setBounds(229, 411, 72, 23);
		getContentPane().add(tf_salary);
		tf_salary.setColumns(10);
		
		lbl9 = new JLabel("Benefit:");
		lbl9.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl9.setBounds(90, 443, 111, 23);
		getContentPane().add(lbl9);
		
		tf_benefit = new JTextField();
		tf_benefit.setEditable(false);
		tf_benefit.setBounds(229, 445, 467, 23);
		getContentPane().add(tf_benefit);
		tf_benefit.setColumns(10);
		
		lbl10 = new JLabel("Company Name:");
		lbl10.setFont(new Font("Tahoma", Font.BOLD, 14));
		lbl10.setBounds(90, 169, 140, 23);
		getContentPane().add(lbl10);
		
		tf_cname = new JTextField();
		tf_cname.setEditable(false);
		tf_cname.setBounds(229, 171, 240, 23);
		getContentPane().add(tf_cname);
		tf_cname.setColumns(10);
		
		lblTo = new JLabel("To");
		lblTo.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblTo.setHorizontalAlignment(SwingConstants.CENTER);
		lblTo.setBounds(336, 375, 72, 23);
		getContentPane().add(lblTo);
		
		btnRemove = new JButton("Remove Job");
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(cb_title.getSelectedItem()=="--------------Select a job advertisement--------------"){
					JOptionPane.showMessageDialog(null, "Please select a job advertisement to delete.");
				}
				else{
				int option = JOptionPane.showConfirmDialog (null, "Are you sure you want to delete this job advertisement?",null, JOptionPane.YES_NO_OPTION);
					if (option == JOptionPane.YES_OPTION ) {
						jct.updateStatusToInactive();
						tf_desc.setText("");
						tf_req.setText("");
						tf_duty.setText("");
						tf_it.setText("");
						tf_et.setText("");
						tf_wst.setText("");
						tf_wet.setText("");
						tf_salary.setText("");
						tf_benefit.setText("");
						tf_cname.setText("");
						cb_title.removeItem(cb_title.getSelectedItem());
						cb_title.setSelectedItem("--------------Select a job advertisement--------------");
						JOptionPane.showMessageDialog(null, "Job advertisement succesfully deleted!");

						CompanySendEmail cse = new CompanySendEmail();
						cse.setVisible(true);
						cse.setTitle("Send Email");
						tf_jobid.setText("");
					}
					else {
						JOptionPane.showMessageDialog(null, "Cancelled.");
					}
				}
			}
		});
		btnRemove.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnRemove.setBounds(227, 479, 127, 32);
		getContentPane().add(btnRemove);
		
		
		try{
		cb_title = new JComboBox();
		cb_title.addItem("--------------Select a job advertisement--------------");
		cb_title.revalidate();
		for(int i=0; i<jct.getAllJobsByCompany().size();i++){
            cb_title.addItem(jct.getAllJobsByCompany().get(i));
            }
		cb_title.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cb_title.revalidate();
				
				jct.getJobDetails();
				
				 if(cb_title.getSelectedItem()==("--------------Select a job advertisement--------------")){
					 
                     tf_salary.setText(null);

             }
			}
		});
		
		cb_title.setBounds(229, 103, 288, 23);
		getContentPane().add(cb_title);
		}
		catch(Exception ex){
			JOptionPane.showMessageDialog(null, ex);
		}
	
		JLabel lblNewLabel_1 = new JLabel("Job ID:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_1.setBounds(90, 135, 140, 23);
		contentPane.add(lblNewLabel_1);
		
		tf_jobid = new JTextField();
		tf_jobid.setEditable(false);
		tf_jobid.setBounds(229, 137, 39, 23);
		contentPane.add(tf_jobid);
		tf_jobid.setColumns(10);
		
		
		
		
		
		
		
		
		menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 724, 24);
		contentPane.add(menuBar);
		
		menuHome = new JMenu("Home");
		menuBar.add(menuHome);
		menuhomescreen=new JMenuItem("Home Screen");
		menuhomescreen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyHomePage chp=new CompanyHomePage();
				chp.setLocationRelativeTo(null);
				chp.setVisible(true);
				chp.setTimer("start");
				chp.setTitle("Company - Home Screen");
				closeWindow();
			}
		});
		menuHome.add(menuhomescreen);
		
		menuJobAd=new JMenu("Job Advertisement");
		menuBar.add(menuJobAd);
		menuCreateJob=new JMenuItem("Create Job Advertisement");
		menuCreateJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JobCreateJob jcj=new JobCreateJob();
				jcj.setLocationRelativeTo(null);
				jcj.setVisible(true);
				jcj.setTitle("Company - Create Job Advertisement ");
				closeWindow();
			}
		});
		menuJobAd.add(menuCreateJob);
		
		JSeparator separator_1 = new JSeparator();
		menuJobAd.add(separator_1);
		menuRemoveJob=new JMenuItem("Remove Job Advertisement");
		menuRemoveJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JobRemoveJob jrj=new JobRemoveJob();
				jrj.setLocationRelativeTo(null);
				jrj.setVisible(true);
				jrj.setTitle("Company - Remove Job Advertisement ");
				closeWindow();
			}
		});
		menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
		menuViewAllJobsByCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
				cvajbc.setLocationRelativeTo(null);
				cvajbc.setVisible(true);
				cvajbc.setTitle("Company - View All Jobs By Company ");
				closeWindow();
			}
		});
		menuJobAd.add(menuViewAllJobsByCompany);
		
		JSeparator separator_2 = new JSeparator();
		menuJobAd.add(separator_2);
		menuJobAd.add(menuRemoveJob);
		
		horizontalGlue = Box.createHorizontalGlue();
		menuBar.add(horizontalGlue);
		
		
		
		lblWelcome = new JLabel();
		lblWelcome.setForeground(new Color(0, 0, 128));
		if (CompanyApp.isUserLogin())
		{
			lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
		}
		else
		{
			lblWelcome.setText("Welcome, Guest! Please Login.");
		}
		menuBar.add(lblWelcome);
		
		horizontalStrut = Box.createHorizontalStrut(20);
		menuBar.add(horizontalStrut);
		
		mnProfile = new JMenu("Profile");
		menuBar.add(mnProfile);
		
		mntmUpdateCompany = new JMenuItem("Update Company");
		mntmUpdateCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CompanyUpdateCompany cuc = new CompanyUpdateCompany();
				cuc.setLocationRelativeTo(null);
				cuc.setVisible(true);
				cuc.setTitle("Company - Update Company Details");
				closeWindow();
			}
		});
		
		JMenuItem mntmViewCompany = new JMenuItem("View Company");
		mntmViewCompany.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CompanyViewDetails cvd= new CompanyViewDetails();
				cvd.setLocationRelativeTo(null);
				cvd.setVisible(true);
				cvd.setTitle("Company - View Company Details");
				closeWindow();
			}
		});
		mnProfile.add(mntmViewCompany);
		
		JSeparator separator_3 = new JSeparator();
		mnProfile.add(separator_3);
		mnProfile.add(mntmUpdateCompany);
		
		
		menuExit = new JMenu("Exit");
		menuBar.add(menuExit);
		menuLogout=new JMenuItem("Logout");
		menuLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CompanyApp.logoutUser();
				
				CompanyLogin loginUI = new CompanyLogin();
				loginUI.setLocationRelativeTo(null);
				loginUI.setVisible(true);
				closeWindow();
			}
		});
		menuExit.add(menuLogout);
	}
	
	public void closeWindow()
	{
		this.dispose();
	}
}
